<?php
namespace King\Controller;
use Think\Controller;
use King\Model\managerModel;
use King\Model\memberModel;
use King\Model\configModel;
use King\Model\tmplModel;
use King\Model\roleModel;
use King\Model\purviewsModel;

class BaseController extends Controller{

	public $uid;
	public $kinguid;
	public $kinginfo;
	
	public $managerModel;
	public $memberModel;
	public $configModel;
	public $tmpl;
	
	public $no_need_login = array(
	    "login/*"
	);//不需要登录的控制器,*匹配所有action
	
	public function __construct() {
	    
	    $this->_getConfig();//获取配置文件
	    
	    C('VIEW_PATH','./Theme/king/');//,//模板文件目录
	    //读取模板id并缓存
	    //TODO
	    $tmpl = S("TMPL");
	    if(empty($tmpl) || 1){
	        $tmpl = $this->_getTmpId();//获取模板id
	        S("TMPL",$tmpl);
	    }
	    $this->tmpl = $tmpl;
	    
	    
	    parent::__construct();
	    
	    $this->managerModel = new managerModel();
	    $this->memberModel = new memberModel();
	    $this->kinguid = session('kinguid');
	    $this->kinginfo = session('kinginfo');
	    $this->assign("kinginfo",$this->kinginfo);
	   
	    $this->_checkLogin();//检查登录
	    
	    $this->updateManagerSession($this->kinguid);//获取最新的用户信息
	    
	    
	    $controller = strtolower(CONTROLLER_NAME);//转成小写
	    $action = strtolower(ACTION_NAME);//转成小写
	    $this->assign('action',$action);
	    $this->assign('controller',$controller);
	    
	    //获取菜单
	    //获取一级菜单
	    //当前角色
	
	   
	        //$quanxian = $roleModel->getRole($where);
	      
	           $purviews = array('20','18','28');
	           //获取一级菜单
	           if($purviews){
	               $purviewsModel = new purviewsModel();
	               $where_p = array(
	                   
	                   "id" => array("IN",$purviews)
	               );
	               $plist = $purviewsModel->getPurviewsList($where_p);
	               $this->assign("topmenu",$plist);
	           }
	           
	     
	   
	    
	   // pr($this->kinginfo);

	    
	    
	    if(!IS_AJAX){
	        layout('Common/layout');
	    }
	}
	
	//获取数据库中的配置文件
	protected function _getConfig(){
	    $this->configModel = new configModel();
	    $where = array(
	        "status" => 1,
	    );
	    $res = $this->configModel->getConfigList($where);
	    if(!empty($res)){
	        foreach($res as $k=>$v){
	            C($v['cname'],$v['cvalue']);
	        }
	    }
	    return true;
	}
	
	//获取数据库中的模板消息id
	protected function _getTmpId(){
	    $tmplModel = new tmplModel();
	    $where = array(
	
	    );
	    $res = $tmplModel->getTmplList($where);
	    $new = array();
	    if(!empty($res)){
	         
	        foreach($res as $k=>$v){
	            $new[$v['tmp_name']] = $v;
	        }
	         
	    }
	    return $new;
	}
	
	
	//检查是否登录
	protected function _checkLogin(){
	    $controller = strtolower(CONTROLLER_NAME);//转成小写
	    $action = strtolower(ACTION_NAME);//转成小写
	    if(in_array($controller."/".$action, $this->no_need_login) || in_array($controller."/*", $this->no_need_login)){
	        //当前控制器不需要登录
	        //判断是不是登录控制器
	        if($controller."/".$action == "login/index"){
	            //当前是登录控制器,跳转到后台首页,判断有没有登录
	            if(!empty($this->kinginfo)){
	                header('Location:/king/index/index');//去后台
	                exit;
	            }
	        }
	        return true;
	    }
	    //当前控制器需要登录,检查是否已经登录
	    if(empty($this->kinginfo)){
	        //没有登录
	        header('Location:/king/login/index');//去登陆
	        exit;
	    }else{
	        //已经登录
	        return true;
	    }
	}

	
	//根据uid获取用户信息
	public function getManagerInfobyUid($uid){
	    $where = array(
	        'uid' => $uid
	    );
	    $res = $this->memberModel->where($where)->find();
	    return $res;
	}
	
	
	//更新用户信息
	public function updateManagerInfo($uid,$data){
	    $where = array(
	        'uid' => $uid
	    );
	    $res = $this->memberModel->where($where)->save($data);
	    return $res;
	}
	
	//刷新session userinfo
	public function updateManagerSession($uid){
	    
	    if(empty($uid)){
	        return false;
	    }
	    
	    $memberinfo = $this->getManagerInfobyUid($uid);
	    session('kinginfo',$memberinfo);
	    $this->kinginfo = $memberinfo;
	    $this->assign('kinginfo',$memberinfo);
	}
	
	//文件上传
	public function iupload($type="qrcode"){
	
	    $upload = new \Think\Upload();
	    $upload->rootPath  =  "/";
	    $upload->savePath  = "/upload/".$type."/";
	    $filrInfo   =   $upload->upload();
	    if($filrInfo == false){
	        $this -> error = $upload -> getError();
	    }
	    return $filrInfo;
	}
	
	public function excel($datastr,$header='',$excel_name=''){

	    $filename = $excel_name.date('Y-m-d').'.csv'; //设置文件名

        header("Content-type:text/csv;charset=utf-8");
        header("Content-Disposition:attachment;filename=".$filename);
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
        header('Expires:0');
        header('Pragma:public');
    
      echo  $content = "\xEF\xBB\xBF".$header.$datastr; //添加BOM

	    
	}
	
	
	

}